package creative.fire.interview.ca;

import java.util.Stack;

//get the deepth of ()[]{}/**/
public class DeepOfChart {
	private char[] left = { '(', '[', '{', '/', '*' };
	private char[] right = { ')', ']', '}', '*', '/' };

	private Stack<Integer> stack = new Stack<Integer>();

	public int getDeepth(String s) {
		char[] cc = s.toCharArray();
		int deepth = 0;

		for (int n = 0; n < cc.length; n++) {
			for (int i = 0; i < 4; i++) {
				if (cc[n] == left[i]) {
					if (i != 3)
						stack.push(i);
					else if (n + 1 < cc.length) {
						if (cc[n + 1] == left[4])
							stack.push(i);
					}
				}
			}
		}

		for (int n = 0; n < cc.length; n++) {
			for (int i = 0; i < 4; i++) {
				if (cc[n] == right[i]) {
					boolean match = false;
					if (i != 3) {
						match = stack.pop() == i;
					} else if (n + 1 < cc.length) {
						if (cc[n + 1] == right[4]) {
							match = stack.pop() == i;
						} else
							continue;
					}
					if (match)
						deepth++;
					else
						return -1;
				}
			}
		}

		return deepth;
	}
	
	public static void main(String[] args) {
		DeepOfChart me=new DeepOfChart();
		String s="([{/*[123456]*/}])";
		System.out.println(me.getDeepth(s));
	}
}
